package com.pragmaticminds.forrest.geometry;

import futbol.forrest.gemetry.*;

public class Function {

    public static double EPSILON = 0.0001;  /*!< Value used for floating point equality tests. */


    public Function() {
    }

    public static double Rad2Deg(double x) {
        return (x * 180 / Math.PI);
    }

    public static double Deg2Rad(double x) {
        return (x * Math.PI / 180);
    }

    public static double atan2Deg(double x, double y) {
        if (Math.abs(x) < EPSILON && Math.abs(y) < EPSILON) {
            return 0.0;
        }

        return Rad2Deg(Math.atan2(x, y));
    }

    public static double tanDeg(double x) {
        return Math.tan(Deg2Rad(x));
    }

    public static double cosDeg(double x) {
        return (Math.cos(Deg2Rad(x)));
    }

    public static double sinDeg(double x) {
        return (Math.sin(Deg2Rad(x)));
    }

    public static int abcFormula(double a, double b, double c, double s1, double s2) {
        double dDiscr = b * b - 4 * a * c;       // discriminant is b^2 - 4*a*c
        if (Math.abs(dDiscr) < EPSILON) {  // if discriminant = 0
            s1 = -b / (2 * a);               //  only one solution
            return 1;
        } else if (dDiscr < 0) // if discriminant < 0
        {
            return 0;
        } //  no solutions
        else {                              // if discriminant > 0
            dDiscr = Math.sqrt(dDiscr);      //  two solutions
            s1 = (-b + dDiscr) / (2 * a);
            s2 = (-b - dDiscr) / (2 * a);
            return 2;
        }
    }

    public static double getBisectorTwoAngles(double angMin, double angMax) {
        // separate sine and cosine part to circumvent boundary problem
        return VecPosition.normalizeAngle(
                atan2Deg((sinDeg(angMin) + sinDeg(angMax)) / 2.0,
                (cosDeg(angMin) + cosDeg(angMax)) / 2.0));
    }
}
